﻿using System.Data;
using System.Data.Common;
using Foothill.Common;
using Foothill.DataAccess.Interface;
using System;
using Foothill.Common.Logging;

namespace Foothill.DataAccess.Repository
{
    public class SystemMessageDataAccess : DbAccessBase, ISystemMessageRepo
    {
        public string GetSystemMessage(SystemMessageCategory category, string msgCode, int cultureId)
        {
            string sql = "SELECT [MessageText] FROM " + GetTableName(category) + " WHERE MessageCode=@msgCode AND CultureId=@cultureId";
            DbCommand dbCommand = FoothillDB.GetSqlStringCommand(sql);

            FoothillDB.AddInParameter(dbCommand, "msgCode", DbType.String, msgCode);
            FoothillDB.AddInParameter(dbCommand, "cultureId", DbType.Int32, cultureId);

            string text = string.Empty;
            try
            {
                text = GetDbString(FoothillDB.ExecuteScalar(dbCommand), string.Empty);
            }
            catch (Exception ex)
            {
                Log.Error(FoothillModule.DataAccess, ex.Message, ex);
            }

            return text;
        }

        private string GetTableName(SystemMessageCategory category)
        {
            if (category == SystemMessageCategory.Security)
            {
                return "SecurityMessage";
            }

            return "WebMessage";
        }
    }
}
